<?php
/**
 * ProjectName: Grapes.
 * Created by LemonZ.
 * FileName: Notice.php
 * Date: 2019/2/14
 * Time: 15:29
 * Description:
 */

namespace app\admin\controller;


use app\common\controller\Admin;
use app\common\model\Notice as NoticeModel;
use think\facade\Request;

class Notice extends Admin
{
    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $noticeModel = new NoticeModel();
        if(Request::isAjax())
        {
            return $noticeModel->tableData(input('param.'));
        }
        return $this->fetch();
    }

    /**
     * 添加公告
     * @return array|mixed
     */
    public function add()
    {
        $this->view->engine->layout(false);
        $noticeModel = new NoticeModel();
        if(Request::isPost())
        {
            return $noticeModel->addData(input('param.'));
        }
        return $this->fetch();
    }

    /**
     * 公告编辑
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $this->view->engine->layout(false);
        $noticeModel = new NoticeModel();
        if(Request::isPost())
        {
            return $noticeModel->saveData(input('param.'));
        }
        $noticeInfo = $noticeModel->where('id',input('param.id/d'))->find();
        if (!$noticeInfo)
        {
            return error_code(10002);
        }
        return $this->fetch('edit',['noticeInfo'=>$noticeInfo]);
    }

    /**
     * 公告软删除
     * @return array
     */
    public function del()
    {
        $result = ['status' => true, 'msg' => '删除成功', 'data'  => ''];
        $noticeModel = new NoticeModel();
        if (!$noticeModel->destroy(input('param.id/d')))
        {
            $result['status'] = false;
            $result['msg'] = '删除失败';
        }
        return $result;
    }
}